<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>复选框</title>
</head>

<body>

    <div class="wrapper">
        <div>
            <input type="checkbox" name="btn" id="btn">
            <label for="btn">全选</label>
            <input type="checkbox" name="btn" id="btn1" checked='checked'>
            <label for="btn1">全不选</label>
        </div>
        <div>
            <input type="checkbox" name="hobby"> 足球 <br>
            <input type="checkbox" name="hobby"> 篮球 <br>
            <input type="checkbox" name="hobby"> 乒乓球 <br>
            <input type="checkbox" name="hobby"> 排球 <br>
            <input type="checkbox" name="hobby"> 地球 <br>
        </div>
    </div>
    <script>
        let btn = document.getElementById('btn');
        let btn1 = document.getElementById('btn1');
        console.log(btn);
        let checkAll = function(even) {
            let b = even.target;
            let parent = b.parentElement;
            let next = parent.nextElementSibling;
            let checkboxList = next.querySelectorAll('input[type=checkbox]');
            if (b.checked) {
                Array.from(checkboxList).forEach(c => {
                    c.checked = true;
                })
                btn1.checked = false;
            }
            if (!b.checked) {
                Array.from(checkboxList).forEach(c => {
                    c.checked = false;
                })
                btn1.checked = true;
            }
        }
        let checkNone = function(even) {
            let b1 = even.target;
            let parent = b1.parentElement;
            let next = parent.nextElementSibling;
            let checkboxList = next.querySelectorAll('input[type=checkbox]');
            if (b1.checked) {
                Array.from(checkboxList).forEach(c => {
                    c.checked = false;
                })
                btn.checked = false;
            }
            if (!b1.checked) {
                Array.from(checkboxList).forEach(c => {
                    c.checked = true;
                })
                btn.checked = true;
            }
        }
        btn.addEventListener('click', checkAll, false);
        btn1.addEventListener('click', checkNone, false);
    </script>
</body>

</html>